package com.elong.android.specialhouse.base;

import android.os.Bundle;
import android.view.View;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BaiduMap.OnMapLongClickListener;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.model.LatLng;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.utils.BDLocationManager;
import com.elong.android.specialhouse.utils.BDMapUtils;
import com.elong.framework.netmid.response.IResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yang.yu
 * @ClassName: BaseMapActivity
 * @Description: 地图基类
 * @date 2015-8-17 下午3:46:53
 */
public abstract class BaseMapActivity extends BaseVolleyActivity<IResponse<?>> implements OnMarkerClickListener, OnMapLongClickListener,
		OnMapClickListener {

	/**
	 * 目的地图层
	 */
	public static final int OVERLAY_TYPE_DESTINATION = 0;
	/**
	 * 酒店图层
	 */
	public static final int OVERLAY_TYPE_HOTELITEM = 1;
	/**
	 * 酒店详情图层
	 */
	public static final int OVERLAY_TYPE_HOTELDETAILS = 2;
	/**
	 * 普通的地图图层
	 */
	public static final int OVERLAY_TYPE_NORMAL = 3;
	/**
	 * 酒店列表缩放级别
	 */
	public static final int ZOOM_LEVEL_HOTEL_LIST = 15;
	/**
	 * 绘制红点地图临界点
	 */
	public static final float ZOOM_LEVEL_DRAW_POINT_VIEW = 14.5f;
	/**
	 * 酒店详情缩放列表
	 */
	public static final int ZOOM_LEVEL_HOTEL_DETAILS = 16;

	protected BaiduMap mBaiduMap = null;
	protected MapView mMapView = null;
	protected Overlay mDestinationOverlay = null;
	/**
	 * 地图图层列表,后退的时候要回收避免内存泄露
	 */
	protected List<Overlay> mHotelItemsOnMap = null;
	/**
	 * 地图弹窗偏移量
	 */
	protected static final int INFOWINDOW_Y_OFFSET = -130;
	/**
	 * 酒店列表数量小于3家时定义为精确搜索
	 */
	protected static final int ADVANCED_SEARCH_NUM = 3;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void setContentView(int layoutResID) {

		// 百度地图初始化必须在setContentView()方法之前执行
		SDKInitializer.initialize(getApplicationContext());

		super.setContentView(layoutResID);
		mMapView = (MapView) findViewById(R.id.mapView);
		if (mMapView != null) {
			mBaiduMap = mMapView.getMap();
			mBaiduMap.setOnMarkerClickListener(this);
			mBaiduMap.setOnMapLongClickListener(this);
			mBaiduMap.setOnMapClickListener(this);
			mBaiduMap.setMyLocationEnabled(true);
			// 去掉地图页面自带放大缩小控件
			mMapView.showZoomControls(false);
			mHotelItemsOnMap = new ArrayList<Overlay>();
		}

	}

	/**
	 * 
	 * @Title: animateToPoint
	 * @Description: 设置地图中心点坐标
	 * @param centerPoint
	 * @return void
	 * @throws
	 */
	protected void animateToPoint(LatLng centerPoint) {
		if (mBaiduMap != null) {
			MapStatus ms = new MapStatus.Builder(mBaiduMap.getMapStatus()).target(centerPoint).build();
			MapStatusUpdate u = MapStatusUpdateFactory.newMapStatus(ms);
			mBaiduMap.animateMapStatus(u);
		}
	}

	/**
	 * 
	 * @Title: zoomToLevel
	 * @Description: 缩放地图到制定的Level
	 * @param level
	 * @throws
	 */
	protected void zoomToLevel(float level) {
		if (mBaiduMap != null) {
			MapStatus ms = new MapStatus.Builder(mBaiduMap.getMapStatus()).zoom(level).build();
			MapStatusUpdate u = MapStatusUpdateFactory.newMapStatus(ms);
			mBaiduMap.animateMapStatus(u);
		}
	}

	/**
	 * 
	 * @Title: updateMyLacationOverlay
	 * @Description: 更新用户当前位置图层
	 * @param
	 * @return void
	 * @throws
	 */
	protected void updateMyLacationOverlay() {
		if (BDLocationManager.getInstance().isLocateSuccess()) {
			BDLocation location = BDLocationManager.getInstance().mCurrentLocation;
			// 添加定位图层
			MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius())
			// 此处设置开发者获取到的方向信息，顺时针0-360
					.direction(100).latitude(location.getLatitude()).longitude(location.getLongitude()).build();
			mBaiduMap.setMyLocationData(locData);
		}
	}

	@Override
	public void onMapLongClick(LatLng latlng) {
	}

	@Override
	public void onMapClick(LatLng latlng) {
		mBaiduMap.hideInfoWindow();
	}

	@Override
	public boolean onMapPoiClick(MapPoi mappoi) {
		return false;
	}

	@Override
	protected void initContentView() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onMarkerClick(Marker marker) {
		Bundle bundle = marker.getExtraInfo();
		if (bundle != null) {
			int overlayType = bundle.getInt(BDMapUtils.OVERLAY_TYPE_KEY);
			if (overlayType == BDMapUtils.OVERLAY_TYPE_HOTELITEM) {
				// 显示酒店信息
				popHotelItemInfo(marker.getZIndex());
				return true;
			} else if (overlayType == BDMapUtils.OVERLAY_TYPE_HOTELDETAILS) {
				// 显示酒店详情
				popHotelDetailsInfo();
			} else if (overlayType == BDMapUtils.OVERLAY_TYPE_NORMAL) {
				// 显示周边信息
				popNormalInfo(marker.getTitle(), marker.getPosition());
			}
		}
		return false;
	}

	/**
	 * 
	 * @Title: showHotelItemInfo
	 * @Description: 显示酒店名称
	 * @param index
	 * @throws
	 */
	public void popHotelItemInfo(int index) {

	}

	/**
	 * 
	 * @Title: popNormalInfo
	 * @Description: 地图弹出普通说明信息
	 * @return void
	 * @throws
	 */
	public void popNormalInfo(String text, LatLng point) {

	}

	/**
	 * 
	 * @Title: popHotelDetailsInfo
	 * @Description: 显示酒店名称
	 * @throws
	 */
	public void popHotelDetailsInfo() {

	}

	/**
	 * 
	 * @Title: createHotelItemOverlay
	 * @Description: 创建酒店覆盖物图层
	 * @return Marker
	 * @throws
	 */
	protected void drawHotelItemOverlay(LatLng point, View view, int index) {
		// 在地图上添加Marker，并显示
		mHotelItemsOnMap.add(mBaiduMap.addOverlay(BDMapUtils.createHotelItemOverlay(point, view, index)));
	}

	/**
	 * 
	 * @Title: drawHotelDetailsOverlay
	 * @Description: 创建酒店位置图层
	 * @param point
	 * @param view
	 * @throws
	 */
	protected void drawHotelDetailsOverlay(LatLng point, View view) {
		if (mBaiduMap != null) {
			mBaiduMap.addOverlay(BDMapUtils.createHotelDetailsOverlay(point, view));
		}
	}

	/**
	 * 
	 * @Title: drawNormalItemOverlay
	 * @Description: 绘制用户自定义图层
	 * @param
	 * @return void
	 * @throws
	 */
	protected void drawNormalItemOverlay(String title, LatLng point, View view) {
		if (mBaiduMap != null) {
			mBaiduMap.addOverlay(BDMapUtils.createCustumizeOverlay(title, point, view));
		}
	}

	/**
	 * 
	 * @Title: drawDestinationOverlay
	 * @Description: 绘制目的地图层
	 * @param point
	 * @param view
	 * @return void
	 * @throws
	 */
	protected void drawDestinationOverlay(LatLng point, View view) {
		if (mDestinationOverlay != null) {
			mDestinationOverlay.remove();
			mDestinationOverlay = null;
		}
		// 在地图上添加Marker，并显示
		mDestinationOverlay = mBaiduMap.addOverlay(BDMapUtils.createDestinationOverlay(point, view));
	}

	/**
	 * 
	 * @Title: recycleHotelItemsMarker
	 * @Description: 回收地图上多个酒店的标记(仅限列表页)
	 * @param
	 * @return void
	 * @throws
	 */
	protected void recycleHotelItemsMarker() {
		// 清空地图上面的标记
		if (mHotelItemsOnMap != null && mHotelItemsOnMap.size() > 0) {
			for (Overlay item : mHotelItemsOnMap) {
				if (item != null) {
					item.remove();
				}
			}

			mHotelItemsOnMap.clear();
			mHotelItemsOnMap = null;
			BDMapUtils.recycleMarkers();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (mMapView != null) {
			mMapView.onPause();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mMapView != null) {
			mMapView.onResume();
		}
	}

	@Override
	public void back() {
		recycleHotelItemsMarker();
		super.back();
	}

	@Override
	protected void onDestroy() {
		// 清空地图上面的标记
		recycleHotelItemsMarker();
		super.onDestroy();
		if (mMapView != null) {
			mMapView.onDestroy();
		}

		mMapView = null;
		mBaiduMap = null;
		mDestinationOverlay = null;
	}
}
